fix: translate RecursionError to StackError in fallback Unpacker.skip() - #727
Merged
Merged
Conversation
skip() was the only unpack entry point in fallback.py that let a RecursionError escape on deeply nested input; unpack() and __next__() both re-raise it as StackError, and the C extension raises StackError on this path too. Callers guarding against adversarial nesting with except StackError/ValueError were unprotected when skipping. Wrap the body the same way unpack() does, leaving _consume() outside the try so the post-failure buffer state matches.
hdimer
marked this pull request as ready for review
August 16, 2026 23:20
methane
approved these changes
Aug 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Finding #6 of #683.
Unpacker.skip()is the one unpack entry point infallback.pythat lets aRecursionErrorescape.unpack()and__next__()both translate it,skip()does not:so on deeply nested input the two disagree:
The C extension has no such gap: its
skip()goes through the sameUnpacker._unpackasunpack(), which raisesStackErroronret == -3(_unpacker.pyx:503). So what a caller sees on this path depends on which implementation is loaded.StackErrorsubclassesValueError, so code that guards against adversarial nesting withexcept StackErrororexcept ValueErroris unprotected on the skip path under the pure-Python implementation, which is the configuration where the recursion is reachable in the first place.The fix
Wrap the body the way
unpack()already does, four lines away:self._consume()stays outside thetry, matchingunpack(), so the buffer state after a failure is the same on both paths. I checked that: after the failuretell()is 0 and a retry raisesStackErroragain, identical tounpack()on the same input.The
exceptcannot mask a user error here. UnderEX_SKIP,_unpackcalls no user-supplied hook: the array and map branches recurse withEX_SKIPwithout touchinglist_hook/object_hook/object_pairs_hook, and theif execute == EX_SKIP: returnshort-circuits beforeext_hook. The only recursion inside thetryis msgpack's own, which makes this guard strictly narrower than the two it copies.read_array_header()/read_map_header()do not need the same treatment:_unpackreturns the header count before it reaches the recursion, so they are unaffected by nesting depth.Tests
The assertion sits next to the existing one-shot
StackErrorcase intest_invalidvalue, since it closes the one-shot vs. streaming gap on the same payload. It fails on main withRecursionError: maximum recursion depth exceededand passes with the change. It is green under the C extension too, where the same input trips the embed stack limit, so it also pins the parity rather than just the fallback.Verified on CPython 3.14: full suite green in both the C-extension and
MSGPACK_PUREPYTHON=1configurations,ruff checkandruff formatclean.Disclosure: I used an AI assistant while investigating and writing this patch. I reproduced the divergence myself, confirmed the fix against both builds, and I stand behind the change.